<template>
  <div class="users">
<Row>
    <Col span="4">
     <Tree :data="treeData" @on-select-change="onSelectChange"  ></Tree>
     </Col>
      <Col span="20">

        <Button type="primary" @click="addData">添加数据</Button>
        <Button type="error" @click="removesData">删除多条</Button>
        <Input search v-model="name" placeholder="请输入关键字" @on-search="searchData"/>

        <Table :columns="columns" :data="listData" @on-selection-change="onSelectionChange"></Table>
        <Page :total="total" show-sizer @on-change="onChangePage" @on-page-size-change="onPageSizeChange" :page-size-opts="[5,10,20,30,40,50]"/>
        <Modal
        v-model    = "modal1"
        title      = "Common Modal dialog box title"
        @on-ok     = "ok"
        @on-cancel = "cancel">
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
            <FormItem label="新闻标题ID" prop="newsId">
                <Input v-model="formValidate.newsId"></Input>
            </FormItem>
            <FormItem label="Title" prop="title">
                <Input v-model="formValidate.title" placeholder="Enter your title"></Input>
            </FormItem>
            <quill-editor v-model="formValidate.content" ref='myQuillEditor'></quill-editor>
            <FormItem>
                <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
                <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
            </FormItem>
        </Form>
    </Modal>

      </Col>

</Row>
 </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import  { Base } from '../mixins/Base'
export default {
components: {
    quillEditor
  },
  mixins:[Base],
  data(){
    return {
        treeData:[],
        module:'news',
      formValidate: {
          newsId:null,
          title    : '',
          content : '',
      },
      ruleValidate: {
          title: [
              { required: true, message: 'The title cannot be empty', trigger: 'blur' }
          ],
          content: [
              { required: true, message: 'content cannot be empty', trigger: 'blur' },
          ],

      },
      columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
              title: '新闻标题',
              key  : 'title'
          },

          {
              title: '评论数',
              key  : 'commentCount'
          },
          {
              title: '发布时间',
              key  : 'date',
              render:(h,params)=>{
                  let time=this.$moment(params.row.date).format('YYYY-MM-DD');
                  return h('div',[
                    h('strong',time)
                  ]);
              }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
                return h('div', [
                    h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginRight: '5px'
                        },
                        on: {
                            click: () => {
                                // this.show(params.index)
                                this.editData(params.row._id);
                            }
                        }
                    }, '修改'),
                    h('Button', {
                        props: {
                            type: 'error',
                            size: 'small'
                        },
                        on: {
                            click: () => {
                                this.removeData(params.row._id);
                            }
                        }
                    }, '删除')
                ]);
            }
        }
      ]
    }
  },
  methods:{
      onSelectChange(node){
        this.formValidate.newsId=node[0]._id;
        this.getData()
      },
      addData(){
          if(this.formValidate.newsId===null){
             alert('请输入一个分类');
          }else{
              this.modal1=true;
          }
      },
      getTreeData(){
          this.axios({
              url:'http://localhost:3000/cate/list/1',
              method:'post',
              data:this.treeData
          }).then(res=>{
              console.log(res.data);
              this.treeData=res.data
          })
      },

  },
  mounted() {
    this.getTreeData()
  },
}
</script>